import java.util.Scanner;

class Zhiyuan {

    String[][] vol; 
    int vol_count; 

    Zhiyuan() {
        vol = new String[30][1 + 6]; 
        vol_count = 0;
    }

// add
    void add(String school, String[] majors) {
        if (vol_count >= 30) {
            System.out.println("已达到最大志愿数，无法添加新的志愿。");
            return;// 结束方法
        }
        if(majors.length>6){
            System.out.println("专业数量超出6个");
            return;
        }
        vol[vol_count][0] = school; 
        for (int i = 0; i < majors.length && i < 6; i++) {
            vol[vol_count][i + 1] = majors[i]; 
        }
        vol_count++;
        System.out.println("志愿已添加: " + school);
    }

// addmajor
    void addmajor(int aindex, String[] majors) {
        int snull = 0;// null数量
        if(vol[aindex][0]==null){
            System.out.println("该索引没有填写相应的院校");
            return;
        }
        for (int i = 1; i < 7; i++) {
            if (vol[aindex][i] == null) {
                snull += 1;
            }
        }
        if (majors.length > snull) {
            System.out.println("已超出最大专业数，无法添加。");
            return;
        }
        for (int i = 0; i < majors.length; i++) {
            vol[aindex][i + 7 - snull] = majors[i];
        }
        System.out.println("专业添加完成。");
    }

// del
    void del(int index) {
        if (index < 0 || index >= vol_count) {
            System.out.println("无效的志愿索引。");
            return;
        }
        for (int i = index; i < vol_count - 1; i++) {
            vol[i] = vol[i + 1]; 
        }
        vol[vol_count - 1] = null; 
        vol_count--;
        System.out.println("志愿已删除。");
    }

// delmajor
    void delmajor(int aindex, int bindex) {
        if (aindex < 0 || aindex >= vol_count) {
            System.out.println("无效的志愿索引。");
            return;
        }
        for (int i = bindex; i < vol[aindex].length - 1; i++) {
            vol[aindex][i] = vol[aindex][i + 1]; 
        }
        vol[aindex][vol[aindex].length - 1] = null; 
        System.out.println("专业已删除。");
        if (vol[aindex][1] == null) {
            del(aindex);
            System.out.println("志愿为空，志愿删除。");
        }
    }

// transmajor
    void transmajor(int aindex, int bindex, int cindex) {
        if (aindex < 0 || aindex >= vol_count) {
            System.out.println("无效的志愿索引。");
            return;
        }
        String temp = vol[aindex][bindex];// 交换1
        vol[aindex][bindex] = vol[aindex][cindex];
        vol[aindex][cindex] = temp; 
        System.out.println("专业已替换。");
    }
//trans
    void trans(int aindex,int _aindex){
        if (aindex < 0 || aindex >= vol_count || _aindex<0 || _aindex>=vol_count) {
            System.out.println("无效的志愿索引。");
            return;
        }
        String temp[] = vol[aindex];
        vol[aindex] = vol[_aindex];
        vol[_aindex] = temp;
    }

// display_vol
    void display_vol() {
        System.out.println("当前志愿列表：");
        for (int i = 0; i < vol_count; i++) {
            System.out.print((i + 1) + ". 学校: " + vol[i][0] + ", 专业: ");
            for (int j = 1; j <= 6; j++) {
                if (vol[i][j] != null) {
                    System.out.print(vol[i][j] + " ");
                }
            }
            System.out.println();
        }
    }
}
class Student extends Zhiyuan{
    String name;
    String age;
    String sex;
    String school;
    Student(String name,String age,String sex,String school){
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.school = school;
    }
//重构display_vol
    @Override
    void display_vol() {
        System.out.println("姓名："+name+"\t"+"年龄："+age+"\t"+"性别："+sex+"\t"+"学校："+school);
        System.out.println("当前志愿列表：");
        for (int i = 0; i < vol_count; i++) {
            System.out.print((i + 1) + ". 学校: " + vol[i][0] + ", 专业: ");
            for (int j = 1; j <= 6; j++) {
                if (vol[i][j] != null) {
                    System.out.print(vol[i][j] + " ");
                }
            }
            System.out.println();   
        }
    }
}
public class app {
    public static void main(String[] args) {
        Student information = new Student("小明","18","男","三中");//info包含個人信息和志願表
        Scanner scanner = new Scanner(System.in);//system.in意思是从控制台获取
        String command;
        System.out.println("   - add：添加新的志愿，系统会提示输入学校名称和专业列表。\r\n" + //
                        "   - del：删除指定的志愿，输入要删除的志愿索引（1 到 当前志愿数量）。\r\n" + //
                        "   - addmajor：为指定志愿添加专业，输入志愿编号和专业列表。\r\n" + //
                        "   - delmajor：删除指定志愿的某个专业，输入志愿编号和专业编号。\r\n" + //
                        "   - trans：交换两个志愿的位置，输入两个志愿的编号。\r\n" + //
                        "   - transmajor：交换指定志愿的两个专业，输入志愿编号和两个专业编号。\r\n" + //
                        "   - display：显示当前志愿列表。\r\n" + //
                        "   - exit：退出程序。");
        while (true) {
            System.out.println("请输入命令（add, addmajor, del, delmajor, display, trans, transmajor, exit）：");
            command = scanner.nextLine();

            switch (command.toLowerCase()) {
                case "add":
                    System.out.println("请输入学校名称：");
                    String school = scanner.nextLine();
                    System.out.println("请输入专业（以逗号分隔）：");
                    String[] majors = scanner.nextLine().split("[,，]");
                    information.add(school, majors);
                    break;

                case "del":
                    System.out.println("请输入要删除的志愿索引（1-" + information.vol_count + "）：");
                    int index = scanner.nextInt() - 1;
                    scanner.nextLine(); 
                    information.del(index);
                    break;

                case "display":
                    information.display_vol();
                    break;

                case "exit":
                    System.out.println("退出程序。");
                    scanner.close();
                    information.display_vol();
                    return;// 结束 main 方法，程序停止执行

                case "delmajor":
                    System.out.println("请输入要删除的专业索引('志愿编号,专业编号'):");
                    String[] a = scanner.nextLine().split("[,，]");
                    int aindex = Integer.parseInt(a[0]) - 1;
                    int bindex = Integer.parseInt(a[1]);
                    information.delmajor(aindex, bindex);
                    break;

                case "transmajor":
                    System.out.println("请输入要交换的专业索引('志愿编号,专业编号1，专业编号2'):");
                    String[] A = scanner.nextLine().split("[,，]");
                    int Aindex = Integer.parseInt(A[0]) - 1;
                    int Bindex = Integer.parseInt(A[1]);
                    int Cindex = Integer.parseInt(A[2]);
                    information.transmajor(Aindex, Bindex, Cindex);
                    break;

                case"trans":
                    System.out.println("请输入志愿编号(数字)：编号1，编号2");
                    String[] addtm = scanner.nextLine().split("[,，]");
                    information.trans(Integer.parseInt(addtm[0])-1,Integer.parseInt(addtm[1])-1);
                    break;
                case "addmajor":
                    System.out.println("请输入志愿编号(数字)：");
                    int aaindex = Integer.parseInt(scanner.nextLine()) - 1;
                    System.out.println("请输入专业列表（以逗号分隔）：");
                    String[] addma = scanner.nextLine().split("[,，]");
                    information.addmajor(aaindex, addma);
                    break;

                default:
                    System.out.println("无效命令，请重试。");
                    break;
            }
        }
    }
}
